<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Examination_model extends MY_Model
{

    public function getExaminationList($per_page,$limit)
    {
        $limit = " limit {$per_page},{$limit}";
        $sql = "SELECT * FROM blues_test_paper WHERE isDel = 0  ORDER BY createTime ASC {$limit}";
        $myExamiationTmpList = $this->_geQuerySql($sql);
        
        return $myExamiationTmpList;
    }
    public function getExaminationListCnt(){
        $sql = "select paperID  from blues_test_paper WHERE isDel = 0 ";
        $query = $this->db->query($sql);
        $query->num_rows();
        return $query->num_rows();
    }

     public function getRuanwenList($filter,$per_page, $limit,$fields)
    {
        $where = " ";
        $limit = " limit {$per_page},{$limit}";
        if(in_array($_SESSION['roleadmin'],[2,4]))
        {
            $where .= " AND operationid={$_SESSION['adminUserInfo']['id']}";
        }
        $sql = "SELECT {$fields} FROM blues_news   WHERE isruanwen=1 AND isDel=0 {$where} ORDER BY addtime desc {$limit}";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function getRuanwenCnt($filter){
        $where = " ";
        if(in_array($_SESSION['roleadmin'],[2,4]))
        {
            $where .= " AND operationid={$_SESSION['adminUserInfo']['id']}";
        }
        $sql = "SELECT count(newsID) cnt FROM blues_news   WHERE isruanwen=1 AND isDel=0 {$where}";
        $query = $this->db->query($sql);
        return $query->row_array()['cnt'];
    }
	public function publish($newsID = 0,$data = [])
	{
		$this->db->where("newsID",$newsID);
		$res = $this->db->update("blues_news",$data);
		return $res;
	}
     //编辑
    public function edit($id,$data = array()){
        if($id){
            $this->db->where("paperID",$id);
            $res = $this->db->update("blues_test_paper",$data);
        }else{
            $res = $this->db->insert("blues_test_paper",$data);
        }
        return $res;
    }

    public function delete($id){
        $this->db->where("questionID",$id);
        $data = array(
            'isDel'=>1
        );
        $res = $this->db->update("blues_question",$data);
        return $res;
    }

     //根据id获取广告信息
    public function getExaminationInfo($id){
        $this->db->where("paperID",$id);
        $query = $this->db->get("blues_test_paper");
        return $query->row_array();
    }

    public function editStatus($data = array()){
        $this->db->where("paperID",$data['id']);
        if($data['type'] == "onlinestatus"){
            $data = array(
                'onlinestatus'=>$data['onlinestatus']
            );
        }
        if($data['type'] == "isDel"){
            $data = array(
                'isDel'=>1
            );
        }

        $res = $this->db->update("blues_test_paper",$data);
        return $res;
    }

    public function editQuestion($questionID, $data = [])
    {
        if($questionID)
        {
            //编辑
            $this->db->where("questionID",$questionID);
            $res = $this->db->update("blues_question",$data);
        }else{
            //添加
            $this->db->insert("blues_question",$data);
            $res = $this->db->insert_id();
        }
        return $res;
    }
	
    public function delOptionByQID($questionID)
    {
        $this->db->where(['questionID'=>$questionID]);
        return $this->db->delete("blues_question_option");
    }
    public function editOption($optionID, $data = [])
    {
        if($optionID)
        {
            //编辑
            $this->db->where("optionID",$optionID);
            $res = $this->db->update($data);
        }else{
            //添加
            $this->db->insert("blues_question_option",$data);
            $res = $this->db->insert_id();
        }
        return $res;
    }

    public function getQuestion($newsID,$data)
    {
		$where = " ";
        if($data['questionType'] && $data['questionType'] !=4 )
        {
            $where .= " AND questionType = {$data['questionType']}";
			$sql = "SELECT * FROM blues_question WHERE newsID = {$newsID} AND isDel=0 {$where} ORDER BY questionID ASC";
        }
		if($data['questionType'] ==4)
        {
           $where .= " AND ques.questionType = 3 AND ans.isright =0 ";
		    $sql = "SELECT ques.newsID,ques.questionID,ques.questionTitle,ques.questionType,ques.answer answer,ans.answer user_answer ,ans.AccountID,ans.Createtime,ans.ID answerID,ans.isright,ques.single_question_point,ques.single_question_money FROM blues_question ques ,blues_question_answer ans WHERE ques.questionID=ans.questionID AND ques.newsID={$newsID}  AND ques.isDel=0 {$where} ORDER BY ans.Createtime ASC ";
        }
		if($data['questionType']== "" )
        {
			$sql = "SELECT * FROM blues_question WHERE newsID = {$newsID} AND isDel=0 {$where} ORDER BY questionID ASC";
        }
		
		
		$query = $this->db->query($sql);
        return $query->result_array();
    }
    public function getQuestionList($newsID,$data)
    {	
		$where = " ";
        if($data['questionType'] && $data['questionType'] !=4)
        {
            $where .= " AND ques.questionType = {$data['questionType']}";
        }
		if($data['questionType'] ==4)
        {
            $where .= " AND ques.questionType = 3 AND ans.isright =0 ";
        }
		
        $sql = "SELECT ques.newsID,ques.questionID,ques.questionTitle,ques.questionType,ques.answer right_answer,ans.answer ,ans.AccountID,ans.Createtime,ans.ID answerID,ans.isright,ques.single_question_point,ques.single_question_money FROM blues_question ques ,blues_question_answer ans WHERE ques.questionID=ans.questionID AND ques.newsID={$newsID}  AND ques.isDel=0 {$where} ORDER BY ans.Createtime ASC ";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
	
	public function getQuestionById($questionID)
	{
		$this->db->where("questionID",$questionID);
		$query  = $this->db->get("blues_question");
		return $query->row_array();
	}
    public function getAllQuestionList($data = array(),$per_page, $limit)
    {
        $where = " ";
        if($data['questionType'])
        {
            $where .= " AND ques.questionType = {$data['questionType']}";
        }
        $limit = " limit {$per_page},{$limit}";
        $sql = "SELECT ques.newsID,ques.questionID,ques.questionTitle,ques.questionType,ques.answer right_answer,ans.answer ,ans.AccountID,ans.Createtime,ans.ID answerID,ans.isright,ques.single_question_point,ques.single_question_money FROM blues_question ques ,blues_question_answer ans WHERE ques.questionID=ans.questionID  AND ques.isDel=0 {$where} ORDER BY ans.Createtime ASC {$limit}";
        $query = $this->db->query($sql);
        return $query->result_array();
    }

    public function getAllQuestionCnt($data = array())
    {
        $where = " ";
        if($data['questionType'])
        {
            $where .= " AND questionType = {$data['questionType']}";
        }
        $sql = "SELECT ques.newsID FROM blues_question ques ,blues_question_answer ans WHERE ques.questionID=ans.questionID  AND ques.isDel=0 {$where} ORDER BY ans.Createtime ASC ";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }
    public function getOptionList($questionID)
    {
       $sql = "SELECT * FROM blues_question_option WHERE questionID={$questionID} ";
        $query = $this->db->query($sql);
        return $query->result_array();

    }




    public function getQuestionInfo_answer($questionID,$newsID,$questionType)
    {
        $sql = "SELECT ques.*,news.all_question_point,news.all_question_money FROM blues_question ques ,blues_news news  WHERE ques.newsID=news.newsID AND ques.questionType={$questionType} AND ques.newsID={$newsID} and ques.questionID = {$questionID} AND ques.isDel=0 AND news.ispublish=1";
        $query = $this->db->query($sql);
        return $query->row_array();
    }
        public function updateAnswer($where,$data)
    {
        $this->db->where($where);
        $res = $this->db->update("blues_question_answer",$data);
        return $res;
    }

    public function getQuestionList_answer($newsID,$fields)
    {
        $sql = "SELECT {$fields} FROM blues_question ques ,blues_news news  WHERE ques.newsID=news.newsID AND ques.newsID = {$newsID} AND ques.isDel=0 AND news.ispublish=1 ORDER BY ques.questionID ASC";
        $query = $this->db->query($sql);
        return $query->result_array();
    }
    public function checkQuestionAnswered($questionID,$AccountID)
    {
        $this->db->where(['questionID'=>$questionID,'AccountID'=>$AccountID]);
        $query = $this->db->get("blues_question_answer");
        return $query->row_array();
    }

    public function getAnsweredUserCnt($newsID)
    {
        $sql = "SELECT id  FROM blues_question_answer WHERE newsID={$newsID} GROUP BY AccountID";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }

    public function daishenheCnt($newsID)
    {
        $sql = "SELECT count(ans.id) cnt FROM blues_question_answer ans ,blues_question ques WHERE ans.questionID=ques.questionID AND  ans.newsID={$newsID} AND ques.questionType=3 AND ans.isright=0";
        $query = $this->db->query($sql);
        return $query->row_array()['cnt'];
    }

}


